package com.openroom.ui.component;

import org.apache.pivot.wtk.BoxPane;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.ButtonPressListener;
import org.apache.pivot.wtk.LinkButton;
import org.apache.pivot.wtk.Slider;
import org.apache.pivot.wtk.SliderValueListener;
import org.apache.pivot.wtk.TextInput;
import org.apache.pivot.wtk.TextInputContentListener;
import org.apache.pivot.wtk.content.ButtonData;

public class EditControl extends BoxPane {

    protected Slider slider;
    protected TextInput textInput;
    protected LinkButton linkBtn;

    protected boolean sliderMode = false;

    public EditControl() {
        slider = new Slider();
        textInput = new TextInput();
        linkBtn = new LinkButton();
        textInput.setMaximumWidth(50);
        textInput.setText("0");
        ButtonData buttonData = new ButtonData();
        buttonData.setIcon("/icons/edit-undo-4.png");
        linkBtn.setButtonData(buttonData);
        slider.setStart(getStartValue());
        slider.setEnd(getEndValue());
        slider.setValue(getInitialValue());
        slider.getSliderValueListeners().add(new SliderValueListener() {
            @Override
            public void valueChanged(Slider slider, int previousValue) {
                sliderMode = true;
                int value = slider.getValue();
                textInput.setText(getTextValue(value));
                onValueChanged(value);
            }

        });

        textInput.getTextInputContentListeners().add(
                new TextInputContentListener.Adapter() {
                    @Override
                    public void textInserted(TextInput textInput, int index,
                            int count) {
                        validateText(textInput);
                    }


                    @Override
                    public void textChanged(TextInput textInput) {
                        if (!sliderMode) {
                            if (validateText(textInput)) {
                                slider.setValue(Integer.parseInt("0"
                                        + textInput.getText()));
                            }
                        }
                        sliderMode = false;
                    }
                });

        linkBtn.getButtonPressListeners().add(new ButtonPressListener() {
            @Override
            public void buttonPressed(Button button) {
                slider.setValue(getInitialValue());
            }
        });
        add(slider);
        add(textInput);
        add(linkBtn);
    }

    protected boolean validateText(TextInput textInput) {
        try {
            String text = textInput.getText();
            if (text == null
                    || text.isEmpty()
                    || text.length() == 0)
                return true;
            int parseInt = getValue(text);
            if (parseInt < slider.getStart()
                    || parseInt > slider.getEnd()) {
                int value = slider.getValue();
                textInput.setText(getTextValue(value));
            } else
                return true;
        } catch (Exception e) {
            e.printStackTrace();
            int value = slider.getValue();
            //textInput.setText(getTextValue(value));
        }
        return false;
    }
    
    protected int getInitialValue() {
        return 0;
    }

    protected int getEndValue() {
        return 100;
    }

    protected int getStartValue() {
        return -100;
    }

    protected String getTextValue(int value) {
        return "" + value;
    }

    protected void onValueChanged(int value) {

    }

    protected int getValue(String text) {
        return Integer.parseInt(text);
    }
}
